package cn.edu.ctbu.api01.restcontroller;

import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.service.StudentService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
@Data


@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    StudentService studentService;
    @GetMapping("list")

    public List<Student> GetAll(){
        return studentService.findAll();
    }

    @GetMapping("/get/{id}")

    public Student get(@PathVariable Integer id){
        Student  student= studentService.findById(id);
        student.setPassword("");
        return student;
    }


//    /**
//     * 读取url:/webapi/student/list
//     * @return
//     */
//    @GetMapping("/get/id")
//
//    public Student get(@PathVariable Long id){
//        return studentService.findAll();
//    }





    /**
     * 示例：http://localhost:8080/webapi/student/2
     * 其中，2表示id
     * @param
     * @return
     * **/
//    @GetMapping(value="{id}")
//    public Student GetById(@PathVariable Integer id){
//        return studentService.findById(id);
//    }
//    @DeleteMapping(value="delete/{id}")
//    public boolean DeleteById(@PathVariable Integer id){
//        studentService.delete(id);
//        return true;
//    }

    @PostMapping(value="add")
    public Integer Add(@RequestBody Student student){
        studentService.save(student);
        return student.getId();
    }
//
//    @PutMapping(value="update")
//    public Integer Update(@RequestBody Student student){
//        studentService.save(student);
//        return student.getId();
//    }

    /**
     * 新增的学生的方法、/webapi/student/insert
     * @param student
     * @return
     */
    @PostMapping(value="/insert")
    public Student insert(Student student){
        Student student1 = studentService.insert(student);
        return student1;
    }
    /**
     * 新增的学生的方法、/webapi/student/insert
     * @param student
     * @return
     */
//    @PutMapping(value="update")
//    public Student update(Student student){
//        Student student1 = studentService.update(student);
//        return student1;
//    }

    /**
     * 删除的学生的方法、/webapi/student/delete
     * @param student
     * @return
     */
//    @DeleteMapping(value = "/delete")
//    public void delete(Student student) {
//        studentService.delete(student);
//    }

    /**
     * 删除学生的方法：/webapi/student/delete/{id}
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.delete(id);
    }


//    @PutMapping("/update")
//    public Student update(Student student) {
//        Student oldStudent=studentService.findById(student.getId());
//        if(StringUtils.isEmpty(student.getNo())){
//            student.setNo(oldStudent.getNo());
//        }
//
//        student= studentService.update(student);
//        return student;
//    }
    @PutMapping("/update")
    public ResponseEntity<Student> update(@RequestBody Student student) {
        Student existingStudent = studentService.findById(student.getId());
        if (existingStudent == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        existingStudent.setName(student.getName());
        existingStudent.setSex(student.getSex());
        existingStudent.setScore(student.getScore());
        Student updatedStudent = studentService.update(existingStudent);
        return new ResponseEntity<>(updatedStudent, HttpStatus.OK);

    }

//    @GetMapping("/select")
//    public Student getStudent(String name){
//        Student student= studentService.findByName(name);
//        return student;
//    }
    // 假设StudentService和StudentRepository已经正确实现和注入

    @GetMapping("/select")
    public ResponseEntity<Student> getStudentByName(@RequestParam String name) {
        Student student = studentService.findByName(name);
        if (student != null) {
            return new ResponseEntity<>(student, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND); // 当学生未找到时返回相应的状态码
        }
    }




}
